package com.example.hxds.bff.customer.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.hxds.bff.customer.controller.form.LoginForm;
import com.example.hxds.bff.customer.controller.form.RegisterNewCustomerForm;
import com.example.hxds.bff.customer.service.CustomerService;
import com.example.hxds.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author XUYANG
 * @Date 2024-03-16 15:28
 */
@RestController
@RequestMapping("/customer")
@Tag(name = "CustomerController",description = "用户web接口")
public class CustomerController {
    @Resource
    CustomerService customerService;
    @PostMapping("/registerNewCustomer")
    @Operation(description = "注册新用户")
    public R registerNewCustomer(@RequestBody @Valid RegisterNewCustomerForm form){
        long customerId=customerService.registerNewCustomer(form);
        StpUtil.login(customerId);
        String token=StpUtil.getTokenInfo().getTokenValue();
        return R.ok().put("token",token);
    }
    @PostMapping("/login")
    @Operation(description = "登录系统")
    public R login(@RequestBody @Valid LoginForm form){
        Long customerId=customerService.login(form);
        if (customerId!=null){
            StpUtil.login(customerId);
            String token=StpUtil.getTokenInfo().getTokenValue();
            return R.ok().put("token",token);
        }
        return R.ok();
    }
}
